package Util::Comments::CommentsForUsers;
use Moose;
use namespace::autoclean;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub get_comments {
	my ($self, $user_id, $page) = @_;
	
	my $user = $self->schema->resultset('User')->find($user_id);
	my @comments;
        unless (defined($page)) {
		@comments = $self->schema->resultset('Comment')->search(
			{'me.comm_item_id' => $user->comm_item_id},
			{
				select => [
					'created',
					'user_id',
					'users.photo',
					'users.username',
					'content'
				],
				as => [qw/
					created
					user_id
					photo
					user_name
					content
				/],
				order_by=>{-desc=>"created"},
				join=>'users'
			}
                );
        } else {
		@comments = $self->schema->resultset('Comment')->search(
			{'me.comm_item_id' => $user->comm_item_id},
			{
				select => [
					'created',
					'user_id',
					'users.photo',
					'users.username',
					'content'
				],
				as => [qw/
					created
					user_id
					photo
					user_name
					content
				/],
				order_by=>{-desc=>"created"},
				page => $page,
				join=>'users'
			}
                );
        }

	return \@comments;
}

sub search {
	my ($self, $comm_id) = @_;
	my @comments = $self->schema->resultset('Comment')->search(
		{'me.comm_item_id' => $comm_id},
		{
			select => [
				'created',
				'user_id',
				'users.photo',
				'users.username',
				'content'
			],
			as => [qw/
				created
				user_id
				photo
				user_name
				content
			/],
			order_by=>{-desc=>"created"},
			join=>'users'
		}
	);

	return \@comments;
}

__PACKAGE__->meta->make_immutable;

1;
